package com.cx.mall.cart.controller;

import com.cx.mall.cart.service.CartService;
import com.cx.mall.common.entity.cart.Cart;
import com.cx.mall.common.util.ResponseVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/cart")
public class CartController {
    @Autowired
    private CartService cartService;
    @PostMapping("/save")
    public ResponseVo save(@RequestBody Cart cart) {
        String id = cart.getSkuId() + cart.getUserId();
        cart.set_id(id);
        cart.setCreateTime(LocalDateTime.now());

        cartService.save(cart);

        return ResponseVo.success();
    }

    @GetMapping("/list/{userId}")
    public ResponseVo<List<Cart>> list(@PathVariable("userId") String userId) {
        List<Cart> carts = cartService.list(userId);
        return ResponseVo.success(carts);
    }

    @DeleteMapping("/delete")
    public ResponseVo delete(@RequestBody List<String> ids) {
        cartService.deleteByIds(ids);
        return ResponseVo.success();
    }
}
